package server_commands;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import server.ServerSocketConnection;
import connection_modul.DatabaseConnection;

public class FINDCommand extends Command {

	public FINDCommand(String parameter) {
		setCode(FIND_CMD);
		setParameter(parameter);
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean executeCommand(ServerSocketConnection connection, DatabaseConnection dbConnection) {

		StringTokenizer tokenizer = new StringTokenizer(getParameter());
		
		String username = tokenizer.nextToken().toLowerCase();
		String findParameter = tokenizer.nextToken();
		String radioParameter = tokenizer.nextToken();
		String checkParameter = tokenizer.nextToken();
		
		StringBuffer SQLQuery = new StringBuffer();
		
		SQLQuery.append("SELECT tblUsers.username, tblUsers.nickname, tblUsers.email, tblUsers.ip_address, tblUsers.local_address, tblUsers.port, tblUsers.status, tblUsers.reg_date, tblUsers.avatar");
		SQLQuery.append(" FROM tblUsers");
		SQLQuery.append(" WHERE tblUsers.username NOT IN (SELECT tblContacts.user_dest FROM tblContacts WHERE tblContacts.user_source='" + username + "')");
		SQLQuery.append(" AND (tblUsers.username NOT LIKE '" + username + "')");
		
		if ( findParameter.equals("*") ) {
			if ( checkParameter.equals("false") ) {
				SQLQuery.append(" AND ");
				SQLQuery.append("(tblUsers.status NOT LIKE 'Offline')");
				SQLQuery.append(" AND tblUsers.username NOT IN (SELECT tblBlocks.user_dest FROM tblBlocks WHERE tblBlocks.user_source='" + username + "')");
			}
		}
		else {
			SQLQuery.append(" AND ");
			SQLQuery.append("(tblUsers." + radioParameter + " LIKE '%" + findParameter + "%')");
			if ( checkParameter.equals("false") ) {
				SQLQuery.append(" AND ");
				SQLQuery.append("(tblUsers.status NOT LIKE 'Offline')");
				SQLQuery.append(" AND tblUsers.username NOT IN (SELECT tblBlocks.user_dest FROM tblBlocks WHERE tblBlocks.user_source='" + username + "')");
			}
		}
		
		try {
			dbConnection.doStatement(SQLQuery.toString());
		
		
			Vector<Vector> findList = new Vector<Vector>(dbConnection.data);
		
			dbConnection.clearDataStore();
		
			String blockListQuery = "SELECT tblBlocks.user_source FROM tblBlocks " +
									"WHERE tblBlocks.user_dest = '" + username + "'";


			dbConnection.doStatement(blockListQuery);
		
		
			Vector<Vector> blockedList = new Vector<Vector>(dbConnection.data);
		
			Iterator iterator = blockedList.iterator();
		
			Vector<String> blockedNames = new Vector<String>();
			while ( iterator.hasNext() ) {
				blockedNames.add( ((Vector<String>)iterator.next()).get(0) ); 
			}
		
		
			connection.setMessage("100 Found " + findList.size() );
		
			for (int i = 0; i < findList.size(); i++) {
				StringBuffer entry = new StringBuffer();
				
				for (int j = 0; j < findList.get(i).size(); j++) {
					
					/*
					 * Status kolona.
					 * Svi korinsnici koji su blokirali ovogo korisnika, prikazuju se
					 * u find listi kao Offline.
					 */
					if (j == 6) {
						if (blockedNames.contains(findList.get(i).get(0))) {
							findList.get(i).set(j, "Offline");
						}
					}
	
					entry.append(findList.get(i).get(j));
					if (j < findList.get(i).size() - 1) {
						entry.append(" ");
					}
	
				}
				
				connection.setMessage(entry.toString());
			}			
			
			dbConnection.clearDataStore();
		
		} catch (SQLException e) {
			dbConnection.doRollBack();
			e.printStackTrace();
			return false;
		}
		
		dbConnection.doCommit();
		
		return true;
	}
	
}

